<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">
      <el-form-item label="海报标题:" prop="title">
        <el-input v-model="queryData.title" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('title')"/>
      </el-form-item>
      <el-form-item label="海报类型:" prop="type">
        <el-select v-model="queryData.type" placeholder="请选择" clearable @clear="formClear('type')"
                   style="width: 200px">
          <el-option label="推荐" :value="0"/>
          <el-option label="喜报" :value="1"/>
          <el-option label="节日" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="$router.push('/posterManage/posterAdd')">新 增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column type="index" label="序号" align="center"/>
      <el-table-column prop="createTime" label="海报图片" align="center" width="150">
        <template slot-scope="{row}">
          <el-image style="width: 100px; height: 100px" :src="row.pictureUrl" fit="cover" :preview-src-list="srcList"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" align="center"/>
      <el-table-column prop="content" label="描述" align="center" show-overflow-tooltip/>
      <el-table-column prop="type" label="类型" align="center" :formatter="(row) => typeList[row.type - 0]"/>
      <el-table-column prop="createTime" label="添加时间" align="center" width="150"/>
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text" @click="entityInfoEvent(row)">查 看</el-button>
          <el-button type="text" @click="deleteEvent(row)">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  export default {
    computed: {
      ...mapGetters(['confirm'])
    },
    name: 'posterList',
    data() {
      return {
        srcList: [],
        typeList: ['推荐' , '喜报', '节日'],
        tableList: [],
        queryData: {
          type: null,
          pageNum: 1,
          pageSize: 10,
          total: 0
        },
      }
    },
    beforeMount() {
      this.getRequest()
    },
    methods: {
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },
      /**
       * 重置事件
       */
      onReset() {
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 列表请求事件
       */
      async getRequest() {
        const queryData = JSON.parse(JSON.stringify(this.queryData))
        delete queryData.provinces
        const res = await this.$ajax.get('/wx/poster/list', { params: queryData })
        this.srcList = res.rows.map(item => item.pictureUrl)
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      /**
       * 删除事件
       */
      deleteEvent(row) {
        this.$confirm('确认要删除该数据吗?', '提示', this.confirm.options).then(async() => {
          await this.$ajax.get(`/wx/poster/remove?id=${row.id}`)
          this.$message.success('操作成功!')
          this.onSubmit()
        })
      },
      /**
       * 跳转详情
       */
      entityInfoEvent(row) {
        this.$router.push(`/posterManage/posterAdd?id=${row?.id || ''}`)
      },
      /**
       * form置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      }
    }
  }
</script>

<style scoped>

</style>
